﻿package bdz.projects.slab.sections {
	import bdz.dms.data.DMSCollection;
	import bdz.dms.vo.ContentVO;
	import bdz.dms.vo.SectionVO;
	import bdz.projects.slab.core.SlabSection;
	import flash.events.Event;
	import as3.common.ui.menu.core.MenuComponent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import as3.projects.slfi.renderers.MediaRenderer;
	import as3.common.ui.menu.TweenedGridList;
	import flash.events.Event;
	import bdz.common.utils.BitmapUtils;
	import flash.text.*;
	import flash.events.MouseEvent;
	import bdz.common.utils.TweenLite;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import flash.utils.*;
	import as3.common.utils.TextAnimator;
	import fl.transitions.easing.*;
	import bdz.projects.slab.renderers.ParagraphText;
	import flash.geom.*;
	import bdz.common.utils.DateUtils;
	import bdz.common.display.DisplayLoader;
	import bdz.common.model.AppEnvironment;
	import flash.net.*;
	/**
	* ...
	* @author Default
	*/
	public class SlabActivityList extends SlabSection {
		private var artistVO:SectionVO;
		private var activeData:Array;
		private var list:TweenedGridList;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var listCover:MovieClip;
		private var infoText:InfoText;
		private var galleryVO:SectionVO;
		private var header:GalleryTitle;
		private var thumb:DisplayLoader;
		private var thumbBG:MovieClip;
		private var pressButton:DownloadPress;
		private var _info:DMSCollection;
		private var nocontent:HomeDesc;
		function SlabActivityList() {
			
		}
		override protected function initializeComponent(evt:Event):void {
			activeData = aboutTextParser(_info.whereProperty("tag", "exhibit_activity"));
			listCover = new MovieClip();
			listCover.x = 12;
			listCover.y = 120;
			BitmapUtils.createBounds(listCover, 620, 300, 0);
			addChild(listCover);
			if(activeData.length){
				createList();
			}else {
				nocontent = new HomeDesc();
				nocontent.x = listCover.x+2;
				nocontent.y = Math.ceil(listCover.y + listCover.height/2)-110;
				nocontent.description = "No details yet.";
				addChild(nocontent);
			}
			super.initializeComponent(evt);
		}
		private function listReady(evt:*):void {
			setUpArrows();
			createExtraDetails();
		}
		private function getDateString(str:String, incyear:Boolean = true):String {
			var dte:Date = new Date(str);
			var dstr:String = "";
			dstr += DateUtils.getMonthString(dte.getMonth()) + " ";
			dstr += dte.getDate() + " ";
			if (incyear) {
				dstr += dte.getFullYear();
			}
			return dstr;
		}
		
		private function createExtraDetails():void {
			infoText = new InfoText();
			infoText.description = ("Gallery Activities: " + artistVO.section_name)
			infoText.x = listCover.x;
			infoText.y = Math.ceil(listCover.y + listCover.height)-80;
			addChild(infoText);
			
			header = new GalleryTitle();
			header.description = gallery.section_name + " - " + getDateString(gallery.getProperty("start_date"), false) + " to " + getDateString(gallery.getProperty("end_date"));
			header.y = infoText.y+infoText.height;
			header.x = listCover.x;
			addChild(header);
			
			thumbBG = new MovieClip();
			addChild(thumbBG);
			
			thumb = new DisplayLoader();
			thumb.addEventListener(Event.COMPLETE, thumbnailLoaded);
			thumb.source = AppEnvironment.getEnv().getSetting("baseURL")+artistVO.getProperty("thumbnail");
			thumb.visible = false;
			addChild(thumb);
		}
		private function thumbnailLoaded(evt:Event):void {
			
			BitmapUtils.createBounds(thumbBG, thumb.width + 10, thumb.height + 10, 1, 0x221e1f);
			thumb.alpha = thumbBG.alpha = 0;
			thumbBG.x = listCover.x + listCover.width - thumbBG.width
			thumbBG.x += 5;
			thumbBG.y = infoText.y;
			thumb.x = thumbBG.x + 5;
			thumb.y = thumbBG.y + 5;
			thumb.visible = true;
			TweenLite.to(thumb, .6, {delay:.1, alpha:1, ease:Strong.easeOut } );
			TweenLite.to(thumbBG, .6, { alpha:1, ease:Strong.easeOut } );
		}
		private function setUpArrows():void {
			if(list && list.maxhscroll>0){
				arrLeft = new arrowLeft();
				arrRight = new arrowRight();
				arrLeft.x = listCover.x-30;
				arrRight.x = listCover.x + listCover.width+30;
				arrLeft.y = arrRight.y = listCover.y+(listCover.height/2)-80;
				arrLeft.visible = false;
				arrRight.alpha = arrLeft.alpha = 0;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
			}
		}
		private function scrollRight(evt:MouseEvent):void{
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
		}
		private function scrollLeft(evt:MouseEvent):void{
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
		}
		private function createList():void {
			var tmp:ParagraphText = new ParagraphText(new Object(), new TextFormat());
			list = new TweenedGridList("bdz.projects.slab.renderers.ParagraphText");
			list.addEventListener("listReady", listReady);
			
			list.y = listCover.y;
			list.x = listCover.x;
			list.xpad = 0;
			list.ypad = 50;
			list.labelField = "_";
			list.viewableColumns = 1;
			list.maxrows = 3;
			list.scrollDuration = .7;
			list.delay = .01;
			list.edgeDelay = .03;
			list.offsetEdges = false;
			
			
			list.arrayItems = activeData;
			list.mask = listCover;
			list.easing = Strong.easeInOut;
			addChild(list);
		}
		private function aboutTextParser(itms:Array):Array{
			var ret:Array = new Array();
			var i:Number = 0;
			for each(var itm:ContentVO in itms) {
				var pstring:String = "";
				pstring += getDateString(itm.getProperty("date"), false) + " - ";
				pstring += itm.content_label;
				pstring += "\n" + itm.getProperty("desc") + "\n";
				ret.push( { id:i, paragraph:pstring } );
				i++;
			}
			return ret;
		}
		public function set info(dcoll:DMSCollection):void {
			_info = dcoll;
		}
		public function get info():DMSCollection {
			return _info;
		}
		public function set gallery(svo:SectionVO):void {
			galleryVO = svo;
		}
		public function get gallery():SectionVO {
			return galleryVO;
		}
		public function set artist(svo:SectionVO):void {
			artistVO = svo;
		}
		public function get artist():SectionVO {
			return artistVO;
		}
	}
	
}